﻿<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1,minimum-scale=1,user-scalable=no">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="themes/base.min.css" />
	<link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
	<link rel="stylesheet" href="themes/jquery.mobile.structure-1.4.5.min.css" />
	<script src="js/jquery-1.11.1.min.js"></script>
	<script src="js/jquery.mobile-1.4.5.min.js"></script>
	<title>分享设置</title>
    <style type="text/css">
        .ui-page-theme-a .ui-btn.changed{
            background-color: #3388cc;
            color: #fff;
        }
    </style>
</head>

<body>
    <div data-role="page" data-theme="a">
		<div data-role="header" data-position="inline">
			<h1>分享特权价设置</h1>
		</div>
		<div data-role="content" data-theme="a">
            <div class="ui-field-contain">
                <label for="rate">特权价折扣率:<span id="rateshow"></span></label>
                <input type="range" name="rate" id="rate" value="50" min="0" max="99" data-highlight="true">
            </div>
			<div class="ui-field-contain">
                <label for="privilege">启用分享特权价:</label>
                <select name="privilege" id="privilege" data-role="slider" data-mini="true">
                    <option value="false">否</option>
                    <option value="true">是</option>
                </select>
            </div>
            <div class="ui-field-contain">
                <button class="ui-btn ui-btn-b" id="btnSubmit">保存</button>
            </div>
		</div>
	</div>
	<script type="text/javascript">
        //防止token变更
        document.cookie.split(" ").join("").split(";").forEach(function(cookie){
            if(cookie.indexOf("token")==0){
                let hostname = window.location.hostname.substring(window.location.hostname.indexOf("."));
                let token = cookie.split("=")[1];
                let fn = function(){
                    document.cookie = "token="+ escape (token) + ";domain="+hostname+";path=/";
                };
                document.addEventListener("mousedown",fn);
                document.addEventListener("touchstart",fn);
                document.addEventListener("click",fn);
            }
        });
		var apiHost = "https://"+window.location.hostname+"/plugin/api";
		var key='haibao.setting.rate';
		function GetQueryString(name) {
			 var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			 var r = window.location.search.substr(1).match(reg);
			 if(r!=null)return  unescape(r[2]); return null;
		}
        
		var app_id = GetQueryString("app_id");
		function load() {
			$.ajax({
				url:apiHost+"/cache.app.get?app_id="+app_id+"&key="+key,
				type:"GET",
				dataType:"json",
				success: function(res){
					if(res) {
                        $('#rate').val(res.rate*100).slider("refresh");
                        $("#privilege").val(String(res.privilege)).slider("refresh");
					}
                    $("#rate").change(function(){
                        $('#btnSubmit').addClass("changed");
                        $("#rateshow").html(Number(($("#rate").val())/10)+"折");
                    });
                    $("#privilege").change(function(){
                        $('#btnSubmit').addClass("changed");
                    });
				},
			});
		}
		
		function save() {
			var privilege = $('#privilege').val();
			var rate = $('#rate').val();
			if (parseFloat(rate).toString() == "NaN") { 
				rate = 0;
			} else if(rate <=0 ) {
				rate = 0;
			} else if(rate>100) {
				rate = 100;
			}
			$.ajax({
				url:apiHost+"/cache.app.set?app_id="+app_id+"&key="+key,
				type:"POST",
				contentType:"text/plain; charset=utf-8",
				data : JSON.stringify({rate:rate/100.0,privilege:privilege=="true"}),
				success: function(){
                    $('#btnSubmit').removeClass("changed");
				},
			});
		}
		$('#btnSubmit').click(save);
		load();
	</script>
</body>

</html>